package com.OnTimeTeam.OnTime;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;


public class GPSManager extends Service implements LocationListener {

	private Context context;
	private LocationManager mgr;
	private Geocoder geocoder;
	
	// flag for GPS status
    boolean isGPSEnabled;
 
    // flag for network status
    boolean isNetworkEnabled;
 
    boolean canGetLocation;
 
    Location location; // location
    double latitude; // latitude
    double longitude; // longitude
 
    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
 
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
 

	public GPSManager(Context context){
		
		this.context = context;
		
		isGPSEnabled = false;
		isNetworkEnabled = false;
		canGetLocation = false;
		latitude = 0;
		longitude = 0;
		mgr = null;
		location = null;
		geocoder = null;
		
		getLocation();
		
	}
	
	
	public Location getLocation(){
		
		try{
			mgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
			
		   // getting GPS status
	       isGPSEnabled = mgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
	
	       // getting network status
	       isNetworkEnabled = mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
	
	       if (!isGPSEnabled && !isNetworkEnabled) {
	           // no network provider is enabled
	       } else {
	           this.canGetLocation = true;
	           // First get location from Network Provider
	           if (isNetworkEnabled) 
	           {
	               mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
	               Log.d("Network", "Network");
	               if (mgr != null) {
	                   location = mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
	                   if (location != null) {
	                       latitude = location.getLatitude();
	                       longitude = location.getLongitude();
	                   }
	               }
	           }
	           // if GPS Enabled get lat/long using GPS Services
	           if (isGPSEnabled) 
	           {
                   mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, this);
                   Log.d("GPS Enabled", "GPS Enabled");
                   
                   if (mgr != null) 
                   {
                       location = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                       if (location != null) 
                       {
                           latitude = location.getLatitude();
                           longitude = location.getLongitude();
                       }
	                   
	               }
	           }
	       }
	
	   } catch (Exception e) {
	       e.printStackTrace();
   } 
	
	return location;
	}
	
	public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }
 
        // return latitude
        return latitude;
    }
 
    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }
 
        // return longitude
        return longitude;
    }
    
	public String getLocationAddress(){
		
		if(location != null)
		{
			
			geocoder = new Geocoder(context, Locale.getDefault());
			List<Address> addresses;
			String address = "";
			try {
				
				addresses = geocoder.getFromLocation(getLatitude(),getLongitude(),1);

				if(addresses != null)
					address = addresses.get(0).getAddressLine(1) + "," + addresses.get(0).getAddressLine(0);
				
				//If we need the state as well, it's in index 2-		
				// "," + addresses.get(0).getAddressLine(2);
			
			} catch (IOException e) {
				
				address = "";
			}		
			
			return address;
		}
		else return "";
	}
	
	public boolean canGetLocation() {
        return this.canGetLocation;
    }
 
   
	public void onLocationChanged(Location location) {
	    if (location != null)
	    {
	        Log.i("SuperMap", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());
	        latitude = location.getLatitude();
	        longitude = location.getLongitude();
	        Log.i("latitude,longitude", ""+latitude+","+longitude);


	    } 
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
